#include "homewindow.h"
#include "toolbar.h"
#include "../logic/user.h"

#include <QVBoxLayout>
#include <QtDebug>
#include <QCloseEvent>

HomeWindow::HomeWindow( PluginHandler * pluginHandler, ThemeManager * themeManager, QWidget * parent ) : WhaleWindow( parent ) {
    this->mPluginHandler = pluginHandler;
    this->mThemeManager = themeManager;

    ToolBar * toolBar = new ToolBar( pluginHandler );
    // toolbar gets added to the window
    this->addToolBar( toolBar );

    pluginHandler->restorePluginStates();

    // tabscreen handles tab displaying
    TabScreen * tabScreen = new TabScreen();
    this->setCentralWidget( tabScreen );

    // tabmanager handles tab addition, removal and saving
    this->mTabManager = new TabManager( tabScreen, pluginHandler );

    // configuration dialog for plugins and themes
    ConfigurationDialog * configurationDialog = new ConfigurationDialog( pluginHandler, themeManager );

    // activate buttons
    connect( toolBar->logoutAction(), SIGNAL( triggered() ), User::instance(), SLOT( logout() ) );
    connect( toolBar->configureAction(), SIGNAL( triggered() ), configurationDialog, SLOT( show() ) );
    connect( pluginHandler, SIGNAL( newTabRequest( WhaleTabBase * ) ), this->mTabManager, SLOT( addTab( WhaleTabBase * ) ) );

    // restore tabs from previous session (if available)
    this->mTabManager->restoreTabs();
}

// manage close event to save open tabs
void HomeWindow::closeEvent( QCloseEvent * event ) {
    this->mTabManager->saveTabs();
    this->mPluginHandler->savePluginStates();
    this->mThemeManager->saveTheme();
    WhaleWindow::closeEvent( event );
}
